var express = require('express');
var router = express.Router();
var tokens = require('../utils/tokens');
var userkeys = require('../utils/userkeys');
var Users = require('../../db/UsersIf');

var LOGIN_CALLBACK = '/user/login';

var passportClient = require('../../lib/passport')({
  hostname: 'passport.test.makeblock.com',
  clientId: 'micdev',
  secret: 'mic'
});

function _getLoginUrl() {
  var url = 'http://passport.test.makeblock.com';
  url += '/oauth2/auth?state=iot&response_type=code&client_id=micdev';
  url += '&redirect_uri=' + encodeURIComponent('http://localhost:3000/user/login');
  return url;
}

function _getLogoutUrl() {
  var url = 'http://passport.test.makeblock.com';
  url += '/logout?redirect=' + encodeURIComponent('http://localhost:3000/user/login');
  return url;
}

router.get('/hello', function(req, res, next) {
  console.log('session.id:' + req.session.id);
  if(req.session.token) {
    console.log('session.token:' + req.session.token);
    req.session.token = 'mytoken';
  }
  res.send('hello');;
});

router.get('/Insert', function(req, res, next) {
  var userid = 9;
  var userkey = 'userkey';
  var username = 'username';
  Users.Insert(userid, userkey, username);
  res.send('mongo Insert');;
});

router.get('/Query', function(req, res, next) {
  var userid = 9;
  Users.Query(userid);
  res.send('mongo Query');;
});

router.get('/Update', function(req, res, next) {
  var userid = 9;
  var userkey = 'userkeyupdate';
  var username = 'usernameupdate';
  Users.Update(userid, userkey, username);
  res.send('mongo Update');;
});

router.get('/Delete', function(req, res, next) {
  var userid = 9;
  Users.Delete(userid);
  res.send('mongo Delete');;
});

router.get('/userkey', function(req, res, next) {
  var userid = 9;
  userkeys.initUser(userid).then(function(userkey) {
    console.log('userkey:' + userkey);
    res.send(userkey);
  }).catch(function(err) {
    console.log(err);
    res.send('failed');
  })
  
});

router.get('/helloworld', function(req, res, next) {
  res.send('<script>alert(\'darling\')</script>');
  
});

router.get('/logout', function(req, res, next) {
  console.log('req.session.id:' + req.session.id);
  console.log('req.session.user:' + req.session.user);
  req.session.user = null;
  console.log('a req.session.id:' + req.session.id);
  console.log('a req.session.user:' + req.session.user);
  // res.redirect(_getLogoutUrl());
  res.send('logout success');
});

router.get('/login', function(req, res, next) {
  console.log('sessionid:' + req.session.id);
  var code = req.query.code;
  if (code) {
    console.log('code:'+ code);
    passportClient.oauth2.getAccessToken(code).then(function(token) {
      console.log(token);
      return passportClient.api.user.me(token.access_token);
    }).then(function(user) {
      req.session.user = user.user;
      console.log('req.session.user:' + req.session.user);
      // var token = tokens.genLoginToken(req.session.user.email);
      // req.session.user.token = token;
      return userkeys.initUser(req.session.user.id);
    }).then(function(user_key) {
      console.log('user_key:' + user_key);
      // Users.Insert(req.session.user.id, user_key, req.session.user.email);
      var iotToken = 'makeblockiottoken@@' + req.session.user.token;
      res.render('error.ejs', {sessionid: req.session.id, userkey: user_key, email: req.session.user.email});
      // res.send(iotToken);
      // res.end('../index.html');
    }).catch(function(err) {
      console.log(err);
      next(err);
    });
  } else {
    console.log('loginUrl:' + _getLoginUrl());
    res.redirect(_getLoginUrl());
  }
});



module.exports = router;